#################################################################################
#
# Makefile
#
# Makefile for the ASTERISK openh323 channel driver
#
# Copyright (c) 2002-2005 InAccess Networks
# Michalis Manousos <manousos@inaccessnetworks.com>
# Dimitris Economou <decon@inaccessnetworks.com>
#
# This file is part of "H.323 support for ASTERISK"
#
# "H.323 support for ASTERISK" is free software;
# you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any later version. 
#
# "H.323 support for ASTERISK" is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details. 
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
#
# $Id: Makefile,v 1.2 2010/05/04 09:02:57 mmanousos Exp $
#
#################################################################################

TARGET=chan_oh323.so
TARGET_OBJ=chan_oh323.o

ifndef SKIP_SUFFIX
	ifeq ($(OH323STAT),1)
		ifeq ($(OH323BUILDTYPE),debug) # Static, debug
			LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/libpt*_d_s.a))
		else                           # Static, opt
			LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/libpt*_r_s.a))
		endif
	else
		ifeq ($(OH323BUILDTYPE),debug) # Shared, debug
			LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/libpt*d.so))
		else                           # Shared, opt
			LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/libpt*r.so))
		endif
	endif
	LIBPTNAME=$(subst lib,,$(basename $(notdir $(LIBPTPATH))))
else
	ifeq ($(OH323STAT),1)
		LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/$(LIBPT_BASE).a))
	else
		LIBPTPATH=$(word 1,$(shell ls $(PWLIBDIR)/lib/$(LIBPT_BASE).so))
	endif
	LIBPTNAME=$(subst lib,,$(basename $(notdir $(LIBPTPATH))))
endif
LIBPT=$(LIBPTNAME)

ifndef SKIP_SUFFIX
	ifeq ($(OH323STAT),1)
		ifeq ($(OH323BUILDTYPE),debug)   # Static, debug
			LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*_d_s.a))
		else
			ifeq ($(OH323BUILDTYPE),opt) # Static, opt
				LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*_r_s.a))
			else                         # Static, opt, no trace
				LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*_n_s.a))
			endif
		endif
	else
		ifeq ($(OH323BUILDTYPE),debug)   # Shared, debug
			LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*d.so))
		else
			ifeq ($(OH323BUILDTYPE),opt) # Shared, opt
				LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*r.so))
			else                         # Shared, opt, no trace
				LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/libh323*n.so))
			endif
		endif
	endif
	LIBH323NAME=$(subst lib,,$(basename $(notdir $(LIBH323PATH))))
else
	ifeq ($(OH323STAT),1)
		LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/$(LIBH323_BASE).a))
	else
		LIBH323PATH=$(word 1,$(shell ls $(OPENH323DIR)/lib/$(LIBH323_BASE).so))
	endif
	LIBH323NAME=$(subst lib,,$(basename $(notdir $(LIBH323PATH))))
endif
LIBH323=$(LIBH323NAME)

ifndef SKIP_EXTLIBS
	EXTLIBS=$(shell $(PWLIBDIR)/make/ptlib-config --libs)
endif

CFLAGS += -pipe -Wstrict-prototypes -Wmissing-prototypes \
		-Wmissing-declarations -D_REENTRANT -D_GNU_SOURCE -fPIC -g -O2 \
		-fomit-frame-pointer -DAST_MODULE=\"chan_oh323.so\"

DRVDEPS=$(shell ls *.h)


#
# Rules
#
build: $(TARGET)

strip:
	$(STRIP) $(STRIPFLAGS) $(TARGET)

install:
	if [ ! -d $(DESTDIR)$(ASTERISKMODDIR) ]; then \
		$(INSTALL) -d $(DESTDIR)$(ASTERISKMODDIR); \
	fi
	$(INSTALL) $(TARGET) $(DESTDIR)$(ASTERISKMODDIR)
	if [ ! -d $(DESTDIR)$(ASTERISKETCDIR) ]; then \
		$(INSTALL) -d $(DESTDIR)$(ASTERISKETCDIR); \
	fi
	if [ ! -f $(DESTDIR)$(ASTERISKETCDIR)/oh323.conf ]; then \
		$(INSTALL) -m 0644 oh323.conf.sample $(DESTDIR)$(ASTERISKETCDIR)/oh323.conf; \
	fi
	@echo
	@echo " +----------   Asterisk-oh323 Installation Complete   ----------+ "
	@echo " +  "
	@echo " +  Channel driver : $(DESTDIR)$(ASTERISKMODDIR)/$(TARGET)  "
	@echo " +     Config file : $(DESTDIR)$(ASTERISKETCDIR)/oh323.conf  "
ifneq ($(OH323STAT),1)
	@echo " +  "
	@echo " +            *Note* Make sure that the following shared libraries  "
	@echo " +                   will be used at run-time:  "
	@echo " +  "
	@echo " +           Pwlib : $(LIBPTPATH)"
	@echo " +        OpenH323 : $(LIBH323PATH)"
	@echo " + OpenH323 wrapper: $(DESTDIR)$(OH323WRAPLIBDIR)/liboh323wrap.so"
else
	@echo " +  "
	@echo " +            *Note* Pwlib/OpenH323/oh323wrap libraries are "
	@echo " +                   statically linked into the channel driver "
endif
	@echo " +  "
	@echo " +--------------------------------------------------------------+ "
	@echo

clean:
	rm -f *.o *.so

$(TARGET): $(TARGET_OBJ)
	@if [ ! -f "$(LIBPTPATH)" ]; then \
		echo "ERROR: No PWLIB library found!"; exit 1; \
	fi
	@if [ ! -f "$(LIBH323PATH)" ]; then \
		echo "ERROR: No OPENH323 library found!"; exit 1; \
	fi
ifeq ($(OH323STAT),1)
	$(CC) -shared -Xlinker -x -g -o $@ $(TARGET_OBJ) \
	-L../wrapper -loh323wrap_s \
	-L$(OPENH323DIR)/lib -l$(LIBH323) \
	-L$(PWLIBDIR)/lib -l$(LIBPT) \
	-lstdc++ $(EXTLIBS)
else
	$(CC) -shared -Xlinker -x -g -o $@ $(TARGET_OBJ) \
	-L../wrapper -loh323wrap \
	-L$(OPENH323DIR)/lib -l$(LIBH323) \
	-L$(PWLIBDIR)/lib -l$(LIBPT) \
	-lstdc++ $(EXTLIBS)
endif

%.o: %.c $(DRVDEPS)
	$(CC) $(CFLAGS) $(ASTERISKINCLUDE) -c -o $@ $<

#################################################################################
